package com.xc.hotelmgr.order.modules.yd.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xc.hotelmgr.order.common.utils.Constant;
import com.xc.hotelmgr.order.common.utils.GeneValidateVQResponse;
import com.xc.hotelmgr.order.common.utils.XmlUtils;
import com.xc.hotelmgr.order.modules.ali.to.common.DailyInfo;
import com.xc.hotelmgr.order.modules.ali.to.validaterq.request.ValidateRQ;
import com.xc.hotelmgr.order.modules.ali.to.validaterq.response.ValidateRQResult;
import com.xc.hotelmgr.order.modules.order.entity.DtOrderChannelsEntity;
import com.xc.hotelmgr.order.modules.order.service.DtOrderChannelsService;
import com.xc.hotelmgr.order.modules.yd.sdk.YdReq;
import com.xc.hotelmgr.order.modules.yd.to.validate.request.RoomRate;
import com.xc.hotelmgr.order.modules.yd.to.validate.request.TrialOrder;
import com.xc.hotelmgr.order.modules.yd.utils.YdConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("ydValidateService")
public class YdValidateService {

    @Autowired
    private DtOrderChannelsService dtOrderChannelsService;

    public String validateRq(ValidateRQ validateRQ){

        TrialOrder trialOrder =new TrialOrder();

        trialOrder.setHotelId(validateRQ.getHotelId().replaceAll("-1",""));
        //会员id
        trialOrder.setMebId(null);
        trialOrder.setRoomTypeId(validateRQ.getRoomTypeId().replaceAll("-1",""));
        trialOrder.setRoomNum(validateRQ.getRoomNum());

        RoomRate roomRate = new RoomRate();
        List<RoomRate> roomRates = new ArrayList<>();
        List<DailyInfo> dailyInfos = validateRQ.getDailyInfos();
        for (DailyInfo dailyInfo:dailyInfos) {
            roomRate.setAccDate(dailyInfo.getDay());
            roomRate.setBreakfastNum(dailyInfo.getBreakFast());
            roomRate.setRoomRate(String.valueOf(dailyInfo.getPrice()));
            roomRates.add(roomRate);
        }
        trialOrder.setRoomRateList(JSON.toJSONString(roomRates));
        trialOrder.setArrival(validateRQ.getCheckIn());
        trialOrder.setDeparture(validateRQ.getCheckOut());
        //一,二级来源，由亚朵提供
        trialOrder.setSource(null);
        trialOrder.setSubSource(null);
        trialOrder.setRoomRateTypeId("28");
        trialOrder.setPersonNum(validateRQ.getCustomerNumber());
        String userName = validateRQ.getAuthenticationToken().getUsername();
        DtOrderChannelsEntity dtOrderChannelsEntity = dtOrderChannelsService.sqlSelectKey(Constant.Channel.Yd.getValue(), userName);
        //构造返回
        ValidateRQResult validateRQResponse = null;
        if (dtOrderChannelsEntity == null) {
            log.error("亚朵：请配置飞猪接口用户名为{}的店铺对应的亚朵渠道账号",userName);
            validateRQResponse = GeneValidateVQResponse.gene(false, validateRQ.getDailyInfos(), "1");
            return XmlUtils.toXML(validateRQResponse);
        }
        Map<String,String> validateMap = JSON.parseObject(JSON.toJSONString(trialOrder),Map.class);
        try {
            String  trialReq= YdReq.req(YdConstant.testTrialOrder,validateMap);
            if (!StringUtils.isBlank(trialReq)){
                validateRQResponse = GeneValidateVQResponse.gene(false, validateRQ.getDailyInfos(), "4");
                log.info("亚朵下单前校验失败,酒店ID：{},房型ID：{},RP:{},返回信息:{}",validateRQ.getHotelId(),validateRQ.getRoomTypeId(),validateRQ.getRatePlanCode(),validateRQResponse);
                return XmlUtils.toXML(validateRQResponse);
            }
            JSONObject trialReqJson = JSONObject.parseObject(trialReq);
            JSONObject result = trialReqJson.getJSONObject("result");
            if (result.getBoolean("trialFlag")){
                validateRQResponse = GeneValidateVQResponse.gene(true, validateRQ.getDailyInfos(), "4");
                log.info("亚朵下单前校验,酒店ID：{},房型ID：{},RP:{},返回信息:{}",validateRQ.getHotelId(),validateRQ.getRoomTypeId(),validateRQ.getRatePlanCode(),validateRQResponse);
            }else {
                validateRQResponse = GeneValidateVQResponse.gene(false, validateRQ.getDailyInfos(), "4");
                log.info("亚朵下单前校验失败,酒店ID：{},房型ID：{},RP:{},返回信息:{}",validateRQ.getHotelId(),validateRQ.getRoomTypeId(),validateRQ.getRatePlanCode(),validateRQResponse);
            }
        }catch (Exception ex) {
            validateRQResponse = GeneValidateVQResponse.gene(false, validateRQ.getDailyInfos(), "1");
            log.error("亚朵下单前校验异常,酒店ID：{},房型ID：{},RP:{},异常信息：{}",validateRQ.getHotelId(),validateRQ.getRoomTypeId(),validateRQ.getRatePlanCode(), ex.toString());
        }
        return XmlUtils.toXML(validateRQResponse);
    }
}
